<template>
  <div>
    <script :id="idName" type="text/plain"></script>
  </div>
</template>
<script>
import {mapGetters, mapActions} from 'vuex'
  export default {
    name: 'UE',
    data () {
      return {
        editor: null,
      }
    },
    props: {
      idName:"",
      defaultMsg: {
        type: String
      },

      config: {
        type: Object
      }
    },
    computed: {
      //映射State
      ...mapGetters([
        'apiUrl'
      ])
    },
    mounted() {
      const _this = this;
      console.log(this.config)
      // alert(_this.defaultMsg)
      this.editor = UE.getEditor(this.idName, this.config); // 初始化UE
      console.log(this.editor)
      // this.editor.destroy();
      
      // this.editor = UE.getEditor(this.idName, this.config); // 初始化UE
      this.editor.addListener("ready",  () => {
        setTimeout(() => {
          this.editor && this.editor.setContent(this.defaultMsg); // 确保UE加载完成后，放入内容。

        }, 1000)

          if (!window.inited) {
            window.inited = true;
            UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
            UE.Editor.prototype.getActionUrl = function(action) {
                 //这里很重要，很重要，很重要，要和配置中的imageActionName值一样
                 if (action == 'uploadimage' ||action== 'uploadscrawl' || action == 'uploadimage') {
                 //这里调用后端我们写的图片上传接口
                   // return 'http://localhost:8080/api.base/ueditorController/upfileImg';
                   // return '/api/ueditorController/upfileImg?action=uploadimage';
                  return _this.apiUrl+'ueditorController/upfileImg';
                  //return 'http://localhost:8080/ue/jsp/controller.jsp';
               }else{
                    return this._bkGetActionUrl.call(this, action);
                     // return '/api/ueditorController/upfileImg?action=uploadimage';
                    // return 'http://localhost:8080/api.base/ueditorController/upfileImg';
               }
           }
          }
      });


      // _this.$emit('sendFather',_this.editor.getContent())
    },
    methods: {
      getUEContent() { // 获取内容方法
        return this.editor.getContent()
      }
    },
    watch:{
      defaultMsg(newVal,oldVal){
        // alert(newVal)
      }
    },
    destroyed() {
      this.editor.destroy();
    }
  }
</script>
